from flask import Blueprint, json,request,jsonify
import re,random
from rediscode import r
from db import db
from werkzeug.security import generate_password_hash,check_password_hash

bp_users = Blueprint('users',__name__)


#用户登录
@bp_users.route('/userM/',methods=['POST','GET','PUT','DELETE'])
def userM():
    if request.method=='POST':
        data = request.data
        if data:
            data = json.loads(request.data)
        else:
            data = request.form
        mobile = data['mobile']
        password = data['password']    
        smscode = data['smscode']
        # print(smscode,type(smscode))
        #验证码 是否过期
        codes = r.getcode(mobile)
        # print('999999999')
        # print(codes,type(codes))
        if not codes:
            return jsonify({'code':10010,'msg':'验证码过期，请重新发送验证码'})
        if smscode != str(codes):
            return jsonify({'code':200,'msg':'验证码有误'})
        # 手机号是否有 有提示从存在 没有注册并加密密码注册
        # print('****************************')
        sql = "select * from users where mobile='%s'" % data['mobile']
        res = db.get_one(sql)
        
        # print(res)
        if res:
            return jsonify({'code':10011,'msg':'该用户已存在'})
        
        sql = "insert into users(`mobile`,`password`) values('%s','%s')" %(mobile,generate_password_hash(password))
        db.insert(sql)
        
        return jsonify({'code':200})
        
    elif request.method=='GET':
        #获取全部用户信息
        sql = "select * from users order by id desc"
        res = db.get_all(sql)
        return jsonify({"code":200,'list':res})
        
    elif request.method=='PUT':
        pass
    else:
        
        type = int(request.args.get('type'))
        #单删
        if type == 1:
            id = int(request.args.get('id'))
            sql = "delete from goods where id=%d" % id
            db.delete(sql)
        else:
            #多删除
            id = request.args.get('id')
            print(id,type)
        
        return jsonify({"code":200})
    
#验证码    
@bp_users.route('/smscode/',methods=['POST','GET','PUT','DELETE'])
def smscode():
    if request.method=='POST':
        data = request.data
        if data:
            data = json.loads(request.data)
        else:
            data = request.form
        password = data['password']
        mobile = data['mobile']
        print(password,mobile)
        #获取注册过的信息
        sql = "select * from users where mobile='%s'"%mobile

        print(sql)
        res = db.get_one(sql)
        print(res)
        if res:
            if check_password_hash(res['password'],password):
                return jsonify({'code':200,'msg':'登录成功','userid':res['id'],'mobile':res['mobile']})
            else:
                return jsonify({'code':10012,'msg':'密码有误'})
        else:
            return jsonify({'code':200,'msg':"用户不存在"})
        

            
            
    elif request.method=='GET':
        mobile= request.args.get('mobile')
        res = re.match(r'^1[3-9][0-9]{9}$',mobile)
        # print(res)
        if res:
            smscode = random.randint(1000,9999)
            r.setexcode(mobile,30,smscode)

        return jsonify({'code':200,'smscode':smscode}) 
    
    elif request.method=='PUT':
        pass
    else:
        pass      
    